Choice |
The choice class is used to create a pop up list of items from which the user may choose. |
WRITE A PROGRAM ON CHOICE.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Choice extends Applet implements ItemListener
{
Choice lang,pack;
String s=" ";
public void init()
{
lang=new Choice();
pack=new Choice();
lang.add("Java");
lang.add("C");
lang.add("C++");
lang.add("Word");
pack.add("Tally");
pack.add("Wings2000");
add(lang);
add(pack);
lang.addItemListener(this);
pack.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
s="Language: ";
s+=lang.getSelectedItem();
g.drawString(s,6,120);
s="package: ";
s+=pack.getSelectedItem();
g.drawString(s,6,140);
}
}
/*<applet code=Choice width=300 height=180>
</applet>*/
List |
The list class provides a compact multiple choice, scrolling selection list. |
WRITE A PROGRAM ON LISTS.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class List1 extends Applet implements ActionListener
{
List lang,pack;
String s=" ";
public void init()
{
lang=new List(3,true);
pack=new List(3,false);
lang.add("Java");
lang.add("C");
lang.add("C++");
pack.add("Word");
pack.add("Tally");
pack.add("Wings2000");
add(lang);
add(pack);
lang.addActionListener(this);
pack.addActionListener(this);
}
public void actionPerformed(ActionEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
int idx[];
s="Language: ";
idx=lang.getSelectedIndexes();
for(int i=0;i<idx.length;i++)
s+=lang.getItem(idx[i])+" ";
g.drawString(s,6,120);
s="Package: ";
s+=pack.getSelectedItem();
g.drawString(s,6,140);
}
}
/*<applet code=List1 width=300 height=180>
</applet>*/